package com.fancy.learncenter.common;

/**
 * Created by hyy on 2017/12/7.
 * describe as
 */

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.text.TextUtils;

import com.fancy.learncenter.ui.fragment.CartoonPicSpeakFragment;
import com.fancy.learncenter.utils.FileUtils;
import com.fancy.learncenter.utils.LogUtil;
import com.fancy.learncenter.utils.ToastUtil;

import java.io.File;
import java.io.IOException;

/**
 * Created by Hyy on 2016/12/1.
 */

public class CustomAudioPlayer {
    String audioUrl;

    public MediaPlayer mediaPlayer;
    boolean isFile;

    public CustomAudioPlayer(String videoUrl, boolean isFile) {
        if (!TextUtils.isEmpty(videoUrl)) {
            mediaPlayer = new MediaPlayer();
            this.audioUrl = videoUrl;
            initPlayer(videoUrl);
        }

    }

    public void initPlayer(String url) {

        if (isFile) {
            setAudioUrl(url);
        } else {
            try {
                mediaPlayer.setDataSource(audioUrl);
                mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

                mediaPlayer.prepareAsync();//prepare之后自动播放
                mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mp) {
                        // 装载完毕 开始播放流媒体
//                        Toast.makeText(MainActivity.this, "开始播放", 0).show();
//                        LogUtil.MyLog("initPlayer", "播放准备纯在");
//                        ToastUtil.show("开始播放");
//                        mediaPlayer.start();

                        if (preparedListener != null) {
                            preparedListener.preparedListener(mp);
                        }
                    }
                });
//            this.start();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    public void setAudioUrl(String audioUrl) {
        File file = new File(audioUrl);
        if (file.exists() && file.length() > 0) {
            LogUtil.MyLog("initPlayer", "文件存在");
            try {
                mediaPlayer.setDataSource(audioUrl);
                mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

                mediaPlayer.prepareAsync();//prepare之后自动播放
                mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mp) {
                        // 装载完毕 开始播放流媒体
//                        mediaPlayer.start();
//                        Toast.makeText(MainActivity.this, "开始播放", 0).show();

                        if (preparedListener != null) {
                            preparedListener.preparedListener(mp);
                        }
                        LogUtil.MyLog("initPlayer", "播放准备纯在");
                    }
                });
//            this.start();
            } catch (IOException e) {
                e.printStackTrace();
            }

        } else {
            ToastUtil.show("文件损坏");

        }
    }

    public void playAudio() {
        mediaPlayer.start();
    }

    public void pauseAudio() {
        mediaPlayer.pause();
    }

    public void stopAudio() {
        mediaPlayer.stop();
        mediaPlayer.release();
    }

    private PreparedListener preparedListener;

    public PreparedListener getPreparedListener() {
        return preparedListener;
    }

    public void setPreparedListener(PreparedListener preparedListener) {
        this.preparedListener = preparedListener;
    }

    public interface PreparedListener {
        abstract void preparedListener(MediaPlayer mp);
    }
}
